package com.atguigu.juc.day02.sync;

import org.openjdk.jol.info.ClassLayout;

import java.util.concurrent.TimeUnit;

public class Demo02LockExpand {

    public static void main(String[] args) throws InterruptedException {
        // 1、创建一个对象作为锁对象
        Object lockObj = new Object();

        // 2、对锁对象进行分析
        System.out.println(ClassLayout.parseInstance(lockObj).toPrintable());

        // 3、当前线程申请锁
        synchronized (lockObj) {
            System.out.println(ClassLayout.parseInstance(lockObj).toPrintable());
        }

        // 4、创建新线程来申请锁
        new Thread(()->{
            synchronized (lockObj) {
                System.out.println(ClassLayout.parseInstance(lockObj).toPrintable());
                /*while (true) {}*/
            }
        }, "thread-a").start();

        // 5、创建新线程申请锁
        new Thread(()->{
            synchronized (lockObj) {

            }
        }, "thread-b").start();

        // 6、主线程打印分析结果
        System.out.println(ClassLayout.parseInstance(lockObj).toPrintable());

        TimeUnit.SECONDS.sleep(3);

        System.out.println(ClassLayout.parseInstance(lockObj).toPrintable());
    }

}
